package com.budayi.common.service.impl;

import com.budayi.common.service.CoreService;
import com.budayi.common.mapper.BaseMapper;
import com.budayi.vo.PageInfo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author LIBOFEI
 * @ClassName CoreServiceImpl
 * @description 抽象类
 * @date 2021/4/20 12:43
 * @Version 1.0
 **/
public abstract class CoreServiceImpl<T> implements CoreService<T> {

    /** *
     * 传入的mapper
     * @author LIBOFEI
     * @date 12:46 2021/4/20
     * @Version  1.0
    **/
    protected BaseMapper<T> baseMapper;
    /** *
     * 传入的实体类
     * @author LIBOFEI
     * @date 12:46 2021/4/20
     * @Version  1.0
     **/
    protected Class<T> clazz;


    /*** *
     提供构造方法，方便它的子类给通用mapper和操作的实体类赋值
    **/
    public CoreServiceImpl(BaseMapper<T> baseMapper, Class<T> clazz) {
        this.baseMapper = baseMapper;
        this.clazz = clazz;
    }


    @Override
    public Long deleteObjectByPrimaryKey(Object id) {
        return baseMapper.deleteObjectByPrimaryKey(id);
    }

    @Override
    public Long deleteObject(T t) {
        return baseMapper.deleteObject(t);
    }

    @Override
    public Long insertObject(T t) {
        return baseMapper.insertObject(t);
    }

    @Override
    public Long updateObjectByPrimaryKey(T t) {
        return baseMapper.updateObjectByPrimaryKey(t);
    }

    @Override
    public Long updateObjectStatusByPrimaryKeyAndStatus(Object id,int status) {
        Map<String,Object> params = new HashMap<>(3);
        params.put("id",id);
        params.put("status",status);
        return baseMapper.updateObjectStatusByPrimaryKeyAndStatus(params);
    }


    @Override
    public T queryObjectByPrimaryKey(Object id) {
        return baseMapper.queryObjectByPrimaryKey(id);
    }

    @Override
    public List<T> queryObjectListByCondition(T t) {
        return baseMapper.queryObjectListByCondition(t);
    }

    @Override
    public List<T> queryPageInfoObjectListByCondition(PageInfo pageInfo, T t) {
        PageHelper.startPage(pageInfo.getCurrentPage(),pageInfo.getPageSize());
        List<T> userList = baseMapper.queryObjectListByCondition(t);
        Page page =(Page) userList;
        pageInfo.setTotalResultSize((int)page.getTotal());
        pageInfo.setTotalPageSize(page.getPages());
        return userList;
    }


    @Override
    public List<T> queryObjectByIds(List<?> ids) {
        return baseMapper.queryObjectByIds(ids);
    }


    @Override
    public Map<Long, T> queryObjectMapByIds(List<?> ids) {
        List<T> ts = queryObjectByIds(ids);
        Map<Long, T> objectMap = ts.stream().collect(Collectors.toMap(
                t -> {
                    Class<?> tClass = t.getClass();
                    try {
                        Field f = tClass.getDeclaredField("id");
                        f.setAccessible(true);
                        Object id = f.get(t);
                        return Long.valueOf(id.toString());
                    } catch (NoSuchFieldException | IllegalAccessException e) {
                        e.printStackTrace();
                    }
                    return null;
                },
                t -> t
        ));
        return objectMap;
    }
}
